<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <link rel="stylesheet"  href="__ROOT__/static/plugins/bootstrap/css/bootstrap.css">
    <script src="__ROOT__/static/plugins/jquery-3.2.1/jquery-3.2.1.js"></script>
    <script  src="__ROOT__/static/plugins/layer/layer.min.js"></script>
</head>

    <script>
        function del(id){
            var goods = {};
            goods.id = id;
            layer.confirm('确定要删除吗?',{
                        btn:['确定','取消']
                },function(){
                $.post("{:url('admin/Goods/remove')}", goods, function(data){
                    if(data > 0){
                        layer.msg('删除成功', {icon:1});
                        setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                    }else{
                        layer.msg('删除失败', {icon:1});
                    }
                });
            });
        }
        function getChild(){
            var params = {};
            params.id = $("#goodsCatId1").val();
            $.post("{:url('admin/Goods/getChild')}",params, function (data) {
                var obj = $.parseJSON(data); // $.parseJSON将格式完好的JSON字符串转为与之对应的JavaScript对象

                var html = [];
                html.push('<option>请选择</option>');
                $(obj).each(function(index,element){
                   html.push('<option value = "'+ this.id +'">'+this.type_name +'</option>');
                });
                   html = html.join('');
                   $('#goodsCatId2').html(html);
                   $('#goodsCatId3').html("<option>请选择</option>");
            });
        }

        function getSecond(){
            var params = {};
            params.id = $("#goodsCatId2").val();

            $.post("{:url('admin/Goods/getChild')}",params, function (data) {
                var obj = $.parseJSON(data); // $.parseJSON将格式完好的JSON字符串转为与之对应的JavaScript对象

                var html = [];
                html.push('<option>请选择</option>');
                $(obj).each(function(index,element){
                    html.push('<option value = "'+ this.id +'">'+this.type_name +'</option>');
                });
                html = html.join('');
                $('#goodsCatId3').html(html);
            });
        }

        // 设置精品
        function batchBest(){
            var html = [];
            $(".check").each(function(){
                if($(this).prop('checked'))html.push($(this).val());
            });
            var res = html.join(",");
            var params = {};
            params.id = res;
            layer.confirm('确认设置精品吗?',function(){
                $.post("{:url('admin/Goods/boutique')}",params,function(data){
                    if(data==1) {
                        layer.msg('设置精品成功', {icon:1});
                        setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                    }else{
                        layer.msg('设置精品失败', {icon: 2});
                    }
                })
            });
        }

        // 取消精品
        function cancelBest(){
            var html = [];
            $(".check").each(function(){
                if($(this).prop('checked'))html.push($(this).val());
            });
            var res = html.join(",");
            var params = {};
            params.id = res;
            layer.confirm('确认取消精品吗?',function(){
                $.post("{:url('admin/Goods/cancelBoutique')}",params,function(data){
                    if(data==1) {
                        layer.msg('取消精品成功', {icon: 1});
                        setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                    }else{
                        layer.msg('取消精品失败', {icon: 1});
                        window.location.href = "{:url('admin/Goods/index')}";
                    }
                })
            });
        }

        // 今日必抢
        function batchRecom(){
            var html = [];
            $(".check").each(function(){
                if($(this).prop('checked'))html.push($(this).val());
            });
            var res = html.join(",");
            var params = {};
            params.id = res;
            layer.confirm('确认设置必抢吗?',function(){
                $.post("{:url('admin/Goods/batchActive')}",params,function(data){
                    if(data==1) {
                        layer.msg('设置必抢成功', {icon: 1});
                        setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);;
                    }else{
                        layer.msg('设置必抢失败', {icon: 1});
                        window.location.href = "{:url('admin/Goods/index')}";
                    }
                })
            });
        }

        // 取消必抢
        function cancelRecom(){
            var html = [];
            $(".check").each(function(){
                if($(this).prop('checked'))html.push($(this).val());
            });
            var res = html.join(",");
            var params = {};
            params.id = res;
            layer.confirm('确认取消必抢吗?',function(){
                $.post("{:url('admin/Goods/cancelActice')}",params,function(data){
                    if(data==1) {
                        layer.msg('取消精品成功', {icon: 1});
                        setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                    }else{
                        layer.msg('取消精品失败', {icon: 1});
                        window.location.href = "{:url('admin/Goods/index')}";
                    }
                })
            });
        }

        //  主题推荐
        function setRecom(){
            var html = [];
            $(".check").each(function(){
                if($(this).prop('checked'))html.push($(this).val());
            });
            var res = html.join(",");
            var params = {};
            params.id = res;
            layer.confirm('确认设置推荐吗?',function(){
                $.post("{:url('admin/Goods/setGoods')}",params,function(data){
                    if(data==1) {
                        layer.msg('设置推荐成功', {icon: 1});
                        setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                    }else{
                        layer.msg('设置推荐失败', {icon: 1});
                        window.location.href = "{:url('admin/Goods/index')}";
                    }
                })
            });
        }
        // 取消主题
      function canceRecom(){
          var html = [];
          $(".check").each(function(){
              if($(this).prop('checked'))html.push($(this).val());
          });
          var res = html.join(",");
          var params = {};
          params.id = res;
          layer.confirm('确认取消主题吗?',function(){
              $.post("{:url('admin/Goods/cancelRecom')}",params,function(data){
                  if(data==1) {
                      layer.msg('取消主题成功', {icon: 1});
                      setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                  }else{
                      layer.msg('取消主题失败', {icon: 1});
                      window.location.href = "{:url('admin/Goods/index')}";
                  }
              })
          });
      }
  </script>
<body>
<form action="" method="post">
    <div class="">
        商品分类：<select id='goodsCatId1' onchange="getChild()" name="">
                    <option value="0">请选择</option>
                    {volist name="second" id="vo"}
                        <option  value="{$vo.id}">{$vo.type_name}</option>
                    {/volist}
                    </select>
                    <select id='goodsCatId2' onchange="getSecond()" name="">
                        <option value="0">请选择</option>
                    </select>
                    <select id='goodsCatId3' name="">
                        <option value="0">请选择</option>
                    </select>

        商品名称: <input type="text" placeholder="请输入搜索关键字" name="goods_name" id="goods_name" class="input" value="{$goods_name}" />
        <button class="btn btn-primary glyphicon glyphicon-search" onclick="search()" >搜索</button>
    </div>
</form>
<div class="padding border-bottom" style="float: right">
    <a class="btn btn-success glyphicon glyphicon-plus" href="{:url('admin/Goods/insert')}"><span> 添加商品</span></a>
</div>
    <table class="wst-list table table-hover table-striped table-bordered">
        <tr>
            <td  colspan='11'><input type="checkbox" id="checkAll"/>全选
                <a href="javascript:void(0)" class="btn btn-default glyphicon glyphicon-trash" id="delselect" onclick="delAll()"> 全删</a>
                <button type="button" class="btn btn-primary glyphicon" onclick='javascript:batchBest()'>设置精品</button>
                <button type="button" class="btn btn-primary glyphicon" onclick='javascript:cancelBest()'>取消精品</button>
                <button type="button" class="btn btn-primary glyphicon" onclick='javascript:batchRecom()'>设置必抢</button>
                <button type="button" class="btn btn-primary glyphicon" onclick='javascript:cancelRecom()'>取消必抢</button>
                <button type="button" class="btn btn-primary glyphicon" onclick='javascript:setRecom()'>设置主题</button>
                <button type="button" class="btn btn-primary glyphicon" onclick='javascript:canceRecom()'>取消主题</button>
            </td>

        </tr>
        <tr>
            <th>商品Id</th>
            <th>商品图片</th>
            <th>商品名称</th>
            <th>商品价格</th>
            <th>商品库存</th>
            <th>今日必抢</th>
            <th>精品推荐</th>
            <th>主题推送</th>
            <th>销量</th>
            <th width="180px">操作</th>
        </tr>
        {volist name='rows' id='vo'}
        <tr>
            <td>
                <input type="checkbox" class="check" name="id" id="ids" value="{$vo.id}"/>
                {$vo.id}
            </td>
            <td><img width="30px" height="30px" src="__ROOT__{$vo.goods_url}"></td>
            <td>{$vo.goods_name}</td>
            <td>{$vo.goods_price}</td>
            <td>{$vo.goods_stock}</td>
            <td>
                {if condition="$vo.is_active"==1}<span class='label label-success'>必抢</span>{/if}
            </td>
            <td>
                {if condition="$vo.is_best"==1}<span class='label label-success'>精品</span>{/if}
            </td>
            <td>
                {if condition="$vo.is_push"==1}<span class='label label-success'>主题</span>{/if}
            </td>
            <td>
                {$vo.initial_stock-$vo.goods_stock}
            </td>
            <td width="100px">
                <a class="btn btn-default glyphicon glyphicon-pencil" href="{:url('admin/Goods/update',array('id'=>$vo.id))}">修改</a>
                <a class="btn btn-default glyphicon glyphicon-trash" href="javascript:;" onclick="del({$vo.id})">删除</a>

                    <a href="{:url('admin/Goods/initialStock',array('id'=>$vo.id))}"><span style="color:#ffffff;">
                        <button class="btn btn-primary glyphicon">商品进货管理</button></span></a>

            </td>
        </tr>
        {/volist}
        <tr>
            <td colspan='11' align='center'>{$page}</td>
        </tr>
    </table>
</body>
    <script>

        // 搜索
        function search(){
            var params = {};
            params.goods_name = $('#goods_name').val();
            $.post("{:url('admin/Goods/index')}",params,function(data){

            });
        }
        //全选
        $("#checkAll").click(function(){
            $("input[name='id']").each(function(){
                if (this.checked) {
                    this.checked = false;
                }
                else {
                    this.checked = true;
                }
            });
        });

        // 批量删除
        function delAll(){
        layer.confirm('确认删除吗',function(index){
            var obj=$('input[type="checkbox"]:checked');
            var html =[];
            $(obj).each(function(){
                html.push($(this).val());
            });
            var arr =html;
            var trim = function(s) { return s.replace(/\s+/g,'');}
            var re =[];
            for(var i = 0, len = arr.length; i < len; i++) {
                if(trim(arr[i]).length > 0) re[re.length] = arr[i];
            }
            var re2 =re.join(',');
            var params = {};
            params.ids = re2;
            $.post("{:url('admin/Goods/delSelect')}",params,function(data){
                if(data==1){
                    layer.msg('删除成功',{icon:1});
                    setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                }else{
                    layer.msg('删除失败',{time:1000,icon:1});
                    window.location.href="{:url('admin/Goods/index')}";
                }
            })
        });
        }
    </script>
</html>